//
//  OrderViewController.m
//  Cheirapsis
//
//  Created by wangning on 15/11/2.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "OrderViewController.h"
#import "Define.h"
#import "HMSegmentedControl.h"
#import "OrderTableViewCell.h"
#import "OrderDetailViewController.h"
#import "Request.h"
#import "MBProgressHUD.h"
#import "LoginViewController.h"

@interface OrderViewController ()<UIAlertViewDelegate>

{
    UIButton *btn;
    OrderTableViewCell *cell;
    NSMutableArray *dataArray;
    UIView *pleaseView;
}
@property (nonatomic, strong) HMSegmentedControl *segmentedControl2;

@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    dataArray = [[NSMutableArray alloc] init];
    [self dateLoade];
    [self pleaseLogin];
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    // Do any additional setup after loading the view from its nib.
    _segmentedControl2 = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"未接单",@"进行中",@"待评价",@"已完成"]];
    _segmentedControl2.frame = CGRectMake(0, 5, WIDTH, 45*F_HEIGHT);
    _segmentedControl2.selectionIndicatorHeight = 4.0f;
    _segmentedControl2.backgroundColor = [UIColor whiteColor];
    _segmentedControl2.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    _segmentedControl2.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    [_segmentedControl2 addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segmentedControl2];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)dateLoade
{
    dataArray = [[NSMutableArray alloc] init];
    [dataArray removeAllObjects];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[Request sharedInstance] getOrderList:nil content:^(NSArray *arr) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (arr) {
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj[@"orderStatus"] isEqualToString:@"未接单"]) {
                    [dataArray addObject:obj];
                }
            }];
            [self.tableView reloadData];
        }
    }];
}


- (void)viewWillAppear:(BOOL)animated{
    delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];;
    [delegate.tabBar showTabBar];
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"userName"] == nil) {
        _tableView.hidden = YES;
        pleaseView.hidden = NO;
        _segmentedControl2.hidden = YES;
    }else{
        _tableView.hidden = NO;
        pleaseView.hidden = YES;
        _segmentedControl2.hidden = NO;
    }
}

- (void)pleaseLogin{
    pleaseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    pleaseView.backgroundColor = ALL233COROL;
    UILabel *promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200*F_HEIGHT, WIDTH, 20*F_HEIGHT)];
    promptLabel.text = @"您还没有登陆，请登陆";
    promptLabel.font = [UIFont systemFontOfSize:15*F_WIDTH];
    promptLabel.textColor = [UIColor blackColor];
    promptLabel.backgroundColor = [UIColor clearColor];
    promptLabel.textAlignment = NSTextAlignmentCenter;
    
    UIButton *loginBtn = [[UIButton alloc] initWithFrame:CGRectMake(120*F_WIDTH, 220*F_HEIGHT, 80*F_WIDTH, 30*F_HEIGHT)];
    loginBtn.backgroundColor = [UIColor colorWithRed:159/255.0 green:203/255.0 blue:239/255.0 alpha:1];
    [loginBtn setTitle:@"点击登陆" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:pleaseView];
    [pleaseView addSubview:promptLabel];
    [pleaseView addSubview:loginBtn];
}

- (void)login:(UIButton *)btn{
    [self.navigationController pushViewController:[[LoginViewController alloc] init] animated:YES];
}

- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl {
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"userName"] == nil) {
        _tableView.hidden = YES;
        [self pleaseLogin];
    }else{
        _tableView.hidden = NO;
        if (segmentedControl.selectedSegmentIndex == 0) {
            [self dateLoade];
        }else if (segmentedControl.selectedSegmentIndex == 1) {
            [dataArray removeAllObjects];
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [[Request sharedInstance] getOrderList:nil content:^(NSArray *arr) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                if (arr) {
                    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([obj[@"orderStatus"] isEqualToString:@"进行中"]) {
                            [dataArray addObject:obj];
                        }
                    }];
                    [self.tableView reloadData];
                }
            }];
        }else if (segmentedControl.selectedSegmentIndex == 2) {
            [dataArray removeAllObjects];
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [[Request sharedInstance] getOrderList:nil content:^(NSArray *arr) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                if (arr) {
                    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([obj[@"orderStatus"] isEqualToString:@"待评价"]) {
                            [dataArray addObject:obj];
                        }
                    }];
                    [self.tableView reloadData];
                }
            }];
        }else if (segmentedControl.selectedSegmentIndex == 3) {
            [dataArray removeAllObjects];
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [[Request sharedInstance] getOrderList:nil content:^(NSArray *arr) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                if (arr) {
                    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([obj[@"orderStatus"] isEqualToString:@"已完成"]) {
                            [dataArray addObject:obj];
                        }
                    }];
                    [self.tableView reloadData];
                }
            }];
        }
        [_tableView reloadData];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell = [[[NSBundle mainBundle]loadNibNamed:@"OrderTableViewCell" owner:self options:nil]lastObject];
    NSDictionary *dic = [dataArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.xiangMuLab.text = dic[@"StoreName"];
    cell.zhuangTaiLab.text = dic[@"anmoKind"];
    cell.zhuangTaiLab.textColor = [UIColor blueColor];
    cell.timeLab.text = [[NSString stringWithFormat:@"%@",dic[@"arriveTime"]] substringToIndex:19];
    cell.dateLab.text = dic[@"storeAddress"];
    cell.teleLab.text = dic[@"storeTel"];
    cell.jiaGeLab.text = [NSString stringWithFormat:@"%@元",dic[@"money"]];
    cell.jiaGeLab.textColor = [UIColor redColor];
    cell.jiaGeLab.font = [UIFont systemFontOfSize:20.0f];
    cell.jiaGeLab.textAlignment = NSTextAlignmentRight;
    
    btn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH-15-80*F_WIDTH, 180, 80*F_WIDTH, 30)];
    [btn setTitle:@"取消订单" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cell addSubview:btn];
    btn.layer.cornerRadius = 5.0f;
     btn.backgroundColor = [UIColor whiteColor];
    btn.layer.masksToBounds = YES;
    btn.layer.borderColor = [[UIColor grayColor] CGColor];
    btn.layer.borderWidth = 2.0f;
    [btn addTarget:self action:@selector(handleBtn) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (dataArray.count == 0){
        return 0;
    }
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 60*F_HEIGHT)];
    headerView.backgroundColor =[UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1];
    
    
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60*F_HEIGHT;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderDetailViewController * vc = [[OrderDetailViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)handleBtn
{
    
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                   message:@"您确定要取消订单吗"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定"
                                              style:UIAlertActionStyleDefault
                                            handler:^(UIAlertAction *action) {
                                            [btn setTitle:@"已取消" forState:UIControlStateNormal];
                                            }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消"
                                              style:UIAlertActionStyleCancel
                                            handler:^(UIAlertAction *action) {
                                                
                                            }]];

    
    [self presentViewController:alert animated:YES completion:nil];
}




@end
